<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fielddescriptor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_proto2_fielddescriptor.js.html">fielddescriptor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Protocol Buffer Field Descriptor class.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.proto2.FieldDescriptor&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line22"></a>goog.require(&#39;goog.string&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * A class which describes a field in a Protocol Buffer 2 Message.
<a name="line28"></a> *
<a name="line29"></a> * @param {Function} messageType Constructor for the message
<a name="line30"></a> *     class to which the field described by this class belongs.
<a name="line31"></a> * @param {number|string} tag The field&#39;s tag index.
<a name="line32"></a> * @param {Object} metadata The metadata about this field that will be used
<a name="line33"></a> *     to construct this descriptor.
<a name="line34"></a> *
<a name="line35"></a> * @constructor
<a name="line36"></a> * @final
<a name="line37"></a> */
<a name="line38"></a>goog.proto2.FieldDescriptor = function(messageType, tag, metadata) {
<a name="line39"></a>  /**
<a name="line40"></a>   * The message type that contains the field that this
<a name="line41"></a>   * descriptor describes.
<a name="line42"></a>   * @type {Function}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.parent_ = messageType;
<a name="line46"></a>
<a name="line47"></a>  // Ensure that the tag is numeric.
<a name="line48"></a>  goog.asserts.assert(goog.string.isNumeric(tag));
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * The field&#39;s tag number.
<a name="line52"></a>   * @type {number}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.tag_ = /** @type {number} */ (tag);
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * The field&#39;s name.
<a name="line59"></a>   * @type {string}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.name_ = metadata.name;
<a name="line63"></a>
<a name="line64"></a>  /** @type {goog.proto2.FieldDescriptor.FieldType} */
<a name="line65"></a>  metadata.fieldType;
<a name="line66"></a>
<a name="line67"></a>  /** @type {*} */
<a name="line68"></a>  metadata.repeated;
<a name="line69"></a>
<a name="line70"></a>  /** @type {*} */
<a name="line71"></a>  metadata.required;
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * If true, this field is a repeating field.
<a name="line75"></a>   * @type {boolean}
<a name="line76"></a>   * @private
<a name="line77"></a>   */
<a name="line78"></a>  this.isRepeated_ = !!metadata.repeated;
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * If true, this field is required.
<a name="line82"></a>   * @type {boolean}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.isRequired_ = !!metadata.required;
<a name="line86"></a>
<a name="line87"></a>  /**
<a name="line88"></a>   * The field type of this field.
<a name="line89"></a>   * @type {goog.proto2.FieldDescriptor.FieldType}
<a name="line90"></a>   * @private
<a name="line91"></a>   */
<a name="line92"></a>  this.fieldType_ = metadata.fieldType;
<a name="line93"></a>
<a name="line94"></a>  /**
<a name="line95"></a>   * If this field is a primitive: The native (ECMAScript) type of this field.
<a name="line96"></a>   * If an enumeration: The enumeration object.
<a name="line97"></a>   * If a message or group field: The Message function.
<a name="line98"></a>   * @type {Function}
<a name="line99"></a>   * @private
<a name="line100"></a>   */
<a name="line101"></a>  this.nativeType_ = metadata.type;
<a name="line102"></a>
<a name="line103"></a>  /**
<a name="line104"></a>   * Is it permissible on deserialization to convert between numbers and
<a name="line105"></a>   * well-formed strings?  Is true for 64-bit integral field types, false for
<a name="line106"></a>   * all other field types.
<a name="line107"></a>   * @type {boolean}
<a name="line108"></a>   * @private
<a name="line109"></a>   */
<a name="line110"></a>  this.deserializationConversionPermitted_ = false;
<a name="line111"></a>
<a name="line112"></a>  switch (this.fieldType_) {
<a name="line113"></a>    case goog.proto2.FieldDescriptor.FieldType.INT64:
<a name="line114"></a>    case goog.proto2.FieldDescriptor.FieldType.UINT64:
<a name="line115"></a>    case goog.proto2.FieldDescriptor.FieldType.FIXED64:
<a name="line116"></a>    case goog.proto2.FieldDescriptor.FieldType.SFIXED64:
<a name="line117"></a>    case goog.proto2.FieldDescriptor.FieldType.SINT64:
<a name="line118"></a>      this.deserializationConversionPermitted_ = true;
<a name="line119"></a>      break;
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  /**
<a name="line123"></a>   * The default value of this field, if different from the default, default
<a name="line124"></a>   * value.
<a name="line125"></a>   * @type {*}
<a name="line126"></a>   * @private
<a name="line127"></a>   */
<a name="line128"></a>  this.defaultValue_ = metadata.defaultValue;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * An enumeration defining the possible field types.
<a name="line134"></a> * Should be a mirror of that defined in descriptor.h.
<a name="line135"></a> *
<a name="line136"></a> * @enum {number}
<a name="line137"></a> */
<a name="line138"></a>goog.proto2.FieldDescriptor.FieldType = {
<a name="line139"></a>  DOUBLE: 1,
<a name="line140"></a>  FLOAT: 2,
<a name="line141"></a>  INT64: 3,
<a name="line142"></a>  UINT64: 4,
<a name="line143"></a>  INT32: 5,
<a name="line144"></a>  FIXED64: 6,
<a name="line145"></a>  FIXED32: 7,
<a name="line146"></a>  BOOL: 8,
<a name="line147"></a>  STRING: 9,
<a name="line148"></a>  GROUP: 10,
<a name="line149"></a>  MESSAGE: 11,
<a name="line150"></a>  BYTES: 12,
<a name="line151"></a>  UINT32: 13,
<a name="line152"></a>  ENUM: 14,
<a name="line153"></a>  SFIXED32: 15,
<a name="line154"></a>  SFIXED64: 16,
<a name="line155"></a>  SINT32: 17,
<a name="line156"></a>  SINT64: 18
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Returns the tag of the field that this descriptor represents.
<a name="line162"></a> *
<a name="line163"></a> * @return {number} The tag number.
<a name="line164"></a> */
<a name="line165"></a>goog.proto2.FieldDescriptor.prototype.getTag = function() {
<a name="line166"></a>  return this.tag_;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Returns the descriptor describing the message that defined this field.
<a name="line172"></a> * @return {goog.proto2.Descriptor} The descriptor.
<a name="line173"></a> */
<a name="line174"></a>goog.proto2.FieldDescriptor.prototype.getContainingType = function() {
<a name="line175"></a>  return this.parent_.getDescriptor();
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Returns the name of the field that this descriptor represents.
<a name="line181"></a> * @return {string} The name.
<a name="line182"></a> */
<a name="line183"></a>goog.proto2.FieldDescriptor.prototype.getName = function() {
<a name="line184"></a>  return this.name_;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Returns the default value of this field.
<a name="line190"></a> * @return {*} The default value.
<a name="line191"></a> */
<a name="line192"></a>goog.proto2.FieldDescriptor.prototype.getDefaultValue = function() {
<a name="line193"></a>  if (this.defaultValue_ === undefined) {
<a name="line194"></a>    // Set the default value based on a new instance of the native type.
<a name="line195"></a>    // This will be (0, false, &quot;&quot;) for (number, boolean, string) and will
<a name="line196"></a>    // be a new instance of a group/message if the field is a message type.
<a name="line197"></a>    var nativeType = this.nativeType_;
<a name="line198"></a>    if (nativeType === Boolean) {
<a name="line199"></a>      this.defaultValue_ = false;
<a name="line200"></a>    } else if (nativeType === Number) {
<a name="line201"></a>      this.defaultValue_ = 0;
<a name="line202"></a>    } else if (nativeType === String) {
<a name="line203"></a>      if (this.deserializationConversionPermitted_) {
<a name="line204"></a>        // This field is a 64 bit integer represented as a string.
<a name="line205"></a>        this.defaultValue_ = &#39;0&#39;;
<a name="line206"></a>      } else {
<a name="line207"></a>        this.defaultValue_ = &#39;&#39;;
<a name="line208"></a>      }
<a name="line209"></a>    } else {
<a name="line210"></a>      this.defaultValue_ = new nativeType;
<a name="line211"></a>    }
<a name="line212"></a>  }
<a name="line213"></a>
<a name="line214"></a>  return this.defaultValue_;
<a name="line215"></a>};
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/**
<a name="line219"></a> * Returns the field type of the field described by this descriptor.
<a name="line220"></a> * @return {goog.proto2.FieldDescriptor.FieldType} The field type.
<a name="line221"></a> */
<a name="line222"></a>goog.proto2.FieldDescriptor.prototype.getFieldType = function() {
<a name="line223"></a>  return this.fieldType_;
<a name="line224"></a>};
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>/**
<a name="line228"></a> * Returns the native (i.e. ECMAScript) type of the field described by this
<a name="line229"></a> * descriptor.
<a name="line230"></a> *
<a name="line231"></a> * @return {Object} The native type.
<a name="line232"></a> */
<a name="line233"></a>goog.proto2.FieldDescriptor.prototype.getNativeType = function() {
<a name="line234"></a>  return this.nativeType_;
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Returns true if simple conversions between numbers and strings are permitted
<a name="line240"></a> * during deserialization for this field.
<a name="line241"></a> *
<a name="line242"></a> * @return {boolean} Whether conversion is permitted.
<a name="line243"></a> */
<a name="line244"></a>goog.proto2.FieldDescriptor.prototype.deserializationConversionPermitted =
<a name="line245"></a>    function() {
<a name="line246"></a>  return this.deserializationConversionPermitted_;
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Returns the descriptor of the message type of this field. Only valid
<a name="line252"></a> * for fields of type GROUP and MESSAGE.
<a name="line253"></a> *
<a name="line254"></a> * @return {goog.proto2.Descriptor} The message descriptor.
<a name="line255"></a> */
<a name="line256"></a>goog.proto2.FieldDescriptor.prototype.getFieldMessageType = function() {
<a name="line257"></a>  goog.asserts.assert(this.isCompositeType(), &#39;Expected message or group&#39;);
<a name="line258"></a>
<a name="line259"></a>  return this.nativeType_.getDescriptor();
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/**
<a name="line264"></a> * @return {boolean} True if the field stores composite data or repeated
<a name="line265"></a> *     composite data (message or group).
<a name="line266"></a> */
<a name="line267"></a>goog.proto2.FieldDescriptor.prototype.isCompositeType = function() {
<a name="line268"></a>  return this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||
<a name="line269"></a>      this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.GROUP;
<a name="line270"></a>};
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>/**
<a name="line274"></a> * Returns whether the field described by this descriptor is repeating.
<a name="line275"></a> * @return {boolean} Whether the field is repeated.
<a name="line276"></a> */
<a name="line277"></a>goog.proto2.FieldDescriptor.prototype.isRepeated = function() {
<a name="line278"></a>  return this.isRepeated_;
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Returns whether the field described by this descriptor is required.
<a name="line284"></a> * @return {boolean} Whether the field is required.
<a name="line285"></a> */
<a name="line286"></a>goog.proto2.FieldDescriptor.prototype.isRequired = function() {
<a name="line287"></a>  return this.isRequired_;
<a name="line288"></a>};
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>/**
<a name="line292"></a> * Returns whether the field described by this descriptor is optional.
<a name="line293"></a> * @return {boolean} Whether the field is optional.
<a name="line294"></a> */
<a name="line295"></a>goog.proto2.FieldDescriptor.prototype.isOptional = function() {
<a name="line296"></a>  return !this.isRepeated_ &amp;&amp; !this.isRequired_;
<a name="line297"></a>};
</pre>


</body>
</html>
